// 导入db模块
const db = require('../config/db');
const aes = require('../common/aes');//加密的工具
// 数据表：db_wallet

// 钱包类
class Wallet {
    // 开通钱包
    // 用户ID,手机号,支付密码
    // user_id, user_phone, wallet_pwd
    create(user_id, user_phone, wallet_pwd) {
        return new Promise((resolve, reject) => {
            let sql = `select * from db_wallet where user_id = '${user_id}'`;
            db.query(sql)
                .then(
                    (rows) => {
                        if (rows.length === 0) {
                            // 创建钱包
                            let sql2 = `insert into db_wallet(user_id,user_phone,wallet_pwd) values('${user_id}','${user_phone}','${wallet_pwd}')`;
                            db.query(sql2)
                                .then(
                                    (result) => {
                                        // console.log("x:",result)
                                        resolve({
                                            code: 200,
                                            msg: '开通钱包成功。',
                                            user_phone
                                        })

                                    },
                                    (err) => {
                                        // console.log(err)
                                        reject({
                                            code: -1,
                                            msg: '开通钱包失败。~~~'
                                        })
                                    }
                                )
                        }
                        else {
                            // 钱包已开通
                            resolve({
                                code: 1,
                                msg: '该账号钱包已开通。',
                                user_phone
                            })
                        }
                    },
                    (err) => {
                        reject({
                            code: -1,
                            msg: '查询钱包信息失败。',
                            err
                        })
                    }
                )
        })
    }

    // 充值
    recharge(user_id,money){
        return new Promise((resolve, reject) => {
            let sql = `select * from db_wallet where user_id = '${user_id}'`;
            db.query(sql)
                .then(
                    (rows) => {
                        if (rows.length == 1) {
                            let wallet_money = rows[0].wallet_money;
                            let user_phone = rows[0].user_phone
                            wallet_money = (wallet_money * 100) + (money - 0) * 100;
                            wallet_money = wallet_money / 100
                            // 设置数据库信息
                            let sql2 = `update db_wallet set wallet_money = '${wallet_money}'  where  user_id= '${user_id}'`;
                           db.query(sql2)
                               .then(
                                   (result) => {
                                       // 充值成功
                                       resolve({
                                           code: 200,
                                           msg: '充值成功。',
                                           wallet_money,
                                           user_phone
                                       })
                                   },
                                   (err) => {
                                       reject({
                                           code: -1,
                                           msg: '发生异常。充值失败~~~',
                                           err
                                       })
                                   }
                               )
                        }
                        else {
                            // 充值异常
                            resolve({
                                code: 1,
                                msg: '账号充值异常。',
                                user_phone
                            })
                        }
                    },
                    (err) => {
                        reject({
                            code: -1,
                            msg: '查询钱包信息失败。',
                            err
                        })
                    }
                )
        })
    }

    // 支付
    payout(user_id, money,wallet_pwd){
        return new Promise((resolve, reject) => {
            // 查询账号
            let sql = `select * from db_wallet where user_id = '${user_id}'`;
            db.query(sql)
                .then(
                    (rows) => {
                        if (rows.length == 1) {

                            // 判断支付密码是否正确
                            if (aes.aesDecrypt(wallet_pwd, rows[0].wallet_pwd)) {
                                let wallet_money = rows[0].wallet_money;
                                wallet_money = wallet_money - (money - 0);
                                if(wallet_money < 0) {
                                    wallet_money = 0;
                                    resolve({
                                        code: 200,
                                        msg: '钱包当前余额为0。',
                                        wallet_money
                                    })
                                    return false;
                                }
                                // 设置钱包余额
                                let sql2 = `update db_wallet set wallet_money = '${wallet_money}'  where  user_id= '${user_id}'`;
                                db.query(sql2)
                                    .then(
                                        (result) => {
                                            // 判断是否修改成功
                                            resolve({
                                                code: 200,
                                                msg: '支付成功。',
                                                wallet_money
                                            })
    
                                        },
                                        (err) => {
                                            reject({
                                                code: -1,
                                                msg: '支付失败。',
                                                err
                                            })
                                        }
                                    )
                            }
                            else {
                                resolve({
                                    code: 404,
                                    msg: '支付密码不正确。'
                                })
                            }

                           
                        }
                        else {
                            resolve({
                                code: 404,
                                msg: '当前账号没有开通钱包。'
                            })
                        }
                    },
                    (err) => {
                        reject({
                            code: -1,
                            msg: '查询钱包信息失败。'
                        })
                    }
                )
        })
    }
    
    // 设置支付密码
    modify_paypwd(user_id,wallet_pwd){
        return new Promise((resolve, reject) => {
            // 查询账号
            let sql = `select * from db_wallet where user_id = '${user_id}'`;
            db.query(sql)
                .then(
                    (rows) => {
                        if (rows.length == 1) {
                            // 设置支付密码
                             let sql2 = `update db_wallet set wallet_pwd = '${wallet_pwd}'  where  user_id= '${user_id}' `;
                             db.query(sql2)
                                 .then(
                                     (result) => {
                                         // 判断是否修改成功
                                         resolve({
                                             code: 200,
                                             msg: '设置支付密码成功。',
                                         })
 
                                     },
                                     (err) => {
                                         reject({
                                             code: -1,
                                             msg: '设置支付密码失败。',
                                             err
                                         })
                                     }
                                 )
                        }
                        else {
                            resolve({
                                code: 404,
                                msg: '当前账号没有开通钱包。'
                            })
                        }
                    },
                    (err) => {
                        reject({
                            code: -1,
                            msg: '查询钱包信息失败。'
                        })
                    }
                )
        })
    }

    // 获取钱包信息
    info(user_id){
        return new Promise((resolve, reject) => {
            let sql = `select wallet_money, user_id, user_phone,wallet_id from db_wallet where user_id = '${user_id}'`;
            db.query(sql)
                .then(
                    (rows) => {
                        if (rows.length == 1) {
                             // 获取钱包信息
                             resolve({
                                code: 200,
                                msg: '获取钱包信息成功。',
                                result: rows
                            })
                        }
                        else {
                            // 充值异常
                            resolve({
                                code: -1,
                                msg: '未开通。'
                            })
                        }
                    },
                    (err) => {
                        reject({
                            code: -1,
                            msg: '查询钱包信息失败。',
                            err
                        })
                    }
                )
        })
    }
}


// 暴露钱包类实例
module.exports = new Wallet();